package com.walk.zworld.ui.activity.my

import android.app.DatePickerDialog
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import com.walk.zworld.R
import com.walk.zworld.mvvm.vm.TodoActivityViewModel
import com.walk.zworld.webView.formatCurrentDate
import com.walk.zworld.webView.stringToCalendar
import com.base.lib.base.view.BaseMvvmActivity
import java.util.*

class AddTodoActivity : BaseMvvmActivity<TodoActivityViewModel>() {

    /**
     * 优先级  重要（1），一般（0）
     */
    private var mPriority = 0
    private var mCurrentDate = formatCurrentDate()

    override fun onBindViewModel(): Class<TodoActivityViewModel> = TodoActivityViewModel::class.java

    override fun onBindLayout(): Int = R.layout.activity_add_todo

    private lateinit var rg_priority: RadioGroup
    private lateinit var rb0: RadioButton
    private lateinit var rb1: RadioButton
    private lateinit var ll_date: LinearLayout
    private lateinit var tv_date: TextView
    private lateinit var et_title: EditText
    private lateinit var et_content: EditText

    override fun initView() {
        rg_priority = findViewById(R.id.rg_priority)
        rb0 = findViewById(R.id.rb0)
        rb1 = findViewById(R.id.rb1)
        ll_date = findViewById(R.id.ll_date)
        tv_date = findViewById(R.id.tv_date)
        et_title = findViewById(R.id.et_title)
        et_content = findViewById(R.id.et_content)


        rg_priority.setOnCheckedChangeListener { group, checkedId ->
            if (checkedId == R.id.rb0) {
                mPriority = 0
                rb0.isChecked = true
                rb1.isChecked = false
            } else if (checkedId == R.id.rb1) {
                mPriority = 1
                rb0.isChecked = false
                rb1.isChecked = true
            }
        }
        ll_date.setOnClickListener {
            var now = Calendar.getInstance()
            if (intent.getStringExtra("type") == "new") {
                now = intent?.getStringExtra("dateStr")?.stringToCalendar() ?: now
            }
            val dpd = DatePickerDialog(
                this, { view, year, month, dayOfMonth ->
                    mCurrentDate = "$year-${month + 1}-$dayOfMonth"
                    tv_date.text = mCurrentDate
                },
                now.get(Calendar.YEAR),
                now.get(Calendar.MONTH),
                now.get(Calendar.DAY_OF_MONTH)
            )
            dpd.show()
        }
    }
    override fun initData() {
        if (intent?.getStringExtra("type") == "edit") {
            setTop("编辑")
            setRight("保存") setOnClickListener@{
                if (getData() == null) return@setOnClickListener
                updateTodo(intent?.getIntExtra("id", 0), getData()!!)
            }
            if (intent?.getStringExtra("title") != null) {
                et_title.setText(intent?.getStringExtra("title"))
            }
            if (intent?.getStringExtra("content") != null) {
                et_content.setText(intent?.getStringExtra("content"))
            }
            if (intent?.getIntExtra("priority", -1) != 1) {
                rb0.isChecked = true
                rb1.isChecked = false
                mPriority = 0
            } else {
                rb0.isChecked = false
                rb1.isChecked = true
                mPriority = 1
            }
            if (intent?.getStringExtra("dateStr") != null) {
                tv_date.text = intent?.getStringExtra("dateStr")
            }
        } else {
            setTop("新增")
            setRight("保存",) setOnClickListener@{
                if (getData() == null) return@setOnClickListener
                addTodo(getData()!!)
            }
            tv_date.text = mCurrentDate
        }
    }

    private fun getData(): MutableMap<String, Any>? {
        val map = mutableMapOf<String, Any>()
        when {
            et_title.text.toString() == "" -> {
                toast("标题不能为空")
                return null
            }
            et_content.text.toString() == "" -> {
                toast("描述内容不能为空")
                return null
            }
            else -> {
                map["title"] = et_title.text.toString()
                map["content"] = et_content.text.toString()
                map["date"] = tv_date.text.toString()
                map["priority"] = mPriority
            }
        }
        return map
    }

    private fun addTodo(map: MutableMap<String, Any>) {
        showInitLoadView()
        mViewModel.addTodo(map).observe(this) {
            hideInitLoadView()
            setResult(RESULT_OK)
            toast("添加成功")
            finish()
        }
    }

    private fun updateTodo(id: Int?, map: MutableMap<String, Any>) {
        if (id == null) return
        showInitLoadView()
        mViewModel.updateTodo(id, map).observe(this) {
            hideInitLoadView()
            setResult(RESULT_OK)
            toast("修改成功")
            finish()
        }
    }


}